package com.oauth.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.provider.endpoint.FrameworkEndpoint;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@FrameworkEndpoint
public class RevokeTokenEndpoint {

    @Autowired
    @Qualifier("consumerTokenServices")
    ConsumerTokenServices consumerTokenServices;

    @RequestMapping("/oauth/logout")
    @ResponseBody
    public String revokeToken(String access_token) {
        JSONObject jsonObject = new JSONObject(true);
        if (consumerTokenServices.revokeToken(access_token)){
            jsonObject.put("code",1);
            jsonObject.put("msg","注销成功");
            return jsonObject.toJSONString();
        }else{
            jsonObject.put("code",1);
            jsonObject.put("msg","注销失败");
            return jsonObject.toJSONString();
        }
    }
}
